package net.sunzc.takephoto.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import net.sunzc.hwfacetest.R;

/**
 * Created by Administrator on 2017/2/9.
 */
public class ViewUtils {
    private static Toast mToast;

    public static void showToast(Context context, String str) {
        if (TextUtils.isEmpty(str)) return;
        if (mToast == null)
            mToast = Toast.makeText(context, str, Toast.LENGTH_SHORT);
        else
            mToast.setText(str);
        new Handler(context.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                mToast.show();
            }
        });
    }

    public static void showDialog(Context context, String title, String msg, boolean cancelable, View view, String bt2, DialogInterface.OnClickListener listener) {
        showDialog(context, title, msg, cancelable, view, context.getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        }, bt2, listener);
    }

    public static ImageView getImageView(Context context, Bitmap bitmap) {
        if (bitmap == null) return null;
        ImageView imageView = new ImageView(context);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(100, 100);
        imageView.setLayoutParams(lp);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setAdjustViewBounds(true);
        imageView.setImageBitmap(bitmap);
        return imageView;
    }

    public static void showDialog(Context context, String title, String msg, boolean cancelable, DialogInterface.OnClickListener listener) {
        showDialog(context, title, msg, cancelable, null, context.getString(android.R.string.ok), listener);
    }

    public static void showDialog(Context context, String title, String msg, boolean cancelable, View view, DialogInterface.OnClickListener listener) {
        showDialog(context, title, msg, cancelable, view, context.getString(android.R.string.ok), listener);
    }

    public static void showDialog(Context context, String msg) {
        showDialog(context, context.getString(R.string.app_name), msg, true, null, "", null, "", null);
    }

    public static void showDialog(Context context, String title, String msg, boolean cancelable, View view, String bt1, DialogInterface.OnClickListener listener1, String bt2, DialogInterface.OnClickListener listener2) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle(title).setMessage(msg);
        if (view != null)
            dialog.setView(view);
        dialog.setCancelable(cancelable);
        if (!TextUtils.isEmpty(bt1))
            dialog.setNegativeButton(bt1, listener1);
        if (!TextUtils.isEmpty(bt2))
            dialog.setPositiveButton(bt2, listener2);
        dialog.show();
    }

}
